package com.sevntu.apvs.timeChart;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;

public class Table {
	final String Headings[] = { "Цепь", "Элемент 1", "№ Элем.", "№ Конт.", "Элемент 2", "№ Элем.", "№ Конт." };
	private final JTable t;

	@SuppressWarnings("serial")
	Table() {
		TableModel tm = new AbstractTableModel()
		{
			public int getColumnCount()
			{
				return 7;
			}

			public int getRowCount()
			{
				if (TimeChartGUI.infoList == null) 
				{
					return 0;
				} 
				else
				{
					return TimeChartGUI.infoList.size();
				}
			}

			@Override
			public String getColumnName(int col) 
			{
				return Headings[col];
			}

			public Object getValueAt(int row, int col)
			{
				if (TimeChartGUI.infoList == null)
				{
					return "";
				}
				else
				{
					switch (col)
					{
					   case 0: return Integer.toString(TimeChartGUI.infoList.get(row).circuit);
					   case 1: return TimeChartGUI.infoList.get(row).firstElement;
					   case 2: return Integer.toString(TimeChartGUI.infoList.get(row).firstNumb);
					   case 3: return Integer.toString(TimeChartGUI.infoList.get(row).firstPin);
					   case 4: return TimeChartGUI.infoList.get(row).secondElement;
					   case 5: return Integer.toString(TimeChartGUI.infoList.get(row).secondNumb);
					   case 6: return Integer.toString(TimeChartGUI.infoList.get(row).secondPin);
					  default: return 0;
					}
				}
			}

			@SuppressWarnings({ "unchecked", "rawtypes" })
			public Class getColumnClass(int c) 
			{
				return getValueAt(0, c).getClass();
			}

			public boolean isCellEditable(int row, int col)
			{
				return false;
			}
		};

		t = new JTable(tm);
		DefaultTableCellRenderer r = (DefaultTableCellRenderer) t.getDefaultRenderer(String.class);
		r.setHorizontalAlignment(JLabel.CENTER);
		t.setColumnSelectionAllowed(false); 
		t.getTableHeader().setReorderingAllowed(false); 
		t.getTableHeader().setResizingAllowed(false); 
		t.getColumnModel().getColumn(1).setPreferredWidth(180);
		t.getColumnModel().getColumn(4).setPreferredWidth(180);
	}

	public JTable getTable()
	{
		return t;
	}
}